package cn.com.anysdk.redis.config;

import cn.com.anysdk.redis.api.IRedisService;
import cn.com.anysdk.redis.factory.RedisServiceFactory;
import cn.com.anysdk.redis.factory.RedisServiceProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redis 自动配置类
 */
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
    @Autowired
    private RedisProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public IRedisService redisService() {
        RedisConfig config = new RedisConfig();
        config.setHost(properties.getHost());
        config.setPort(properties.getPort());
        config.setPassword(properties.getPassword());
        config.setDatabase(properties.getDatabase());
        config.setTimeout(properties.getTimeout());
        config.setMaxTotal(properties.getMaxTotal());
        config.setMaxIdle(properties.getMaxIdle());
        config.setMinIdle(properties.getMinIdle());
        config.setMaxWaitMillis(properties.getMaxWaitMillis());
        config.setTestOnBorrow(properties.isTestOnBorrow());
        config.setTestOnReturn(properties.isTestOnReturn());
        config.setTestWhileIdle(properties.isTestWhileIdle());

        return RedisServiceProxyFactory.createProxy(
                RedisServiceFactory.createService(config)
        );
    }
} 